var t = require;
var e = module;
var o = exports;
Object.defineProperty(o, "__esModule", {value: !0}),
    (o.AVM1 = void 0),
    (function (t) {
        var e = (function () {
            return function () {};
        })();
        function o(t) {
            for (var o = t.match(/^(\w+)(?:\s+(.*))?$/), n = o[2] ? o[2].split(",") : [], r = n.length; r--; ) {
                var a = n[r],
                    i = a.match(/^label\d+$/);
                if (!i)
                    if ((i = a.match(/^registerid=(\d+)$/))) n[r] = parseInt(i[1]);
                    else if ((i = a.match(/^r\:(\d+)$/))) {
                        var s = new e();
                        (s.index = parseInt(i[1])), (n[r] = s);
                    } else if ((i = a.match('^"(.+)"$'))) n[r] = i[1];
                    else
                        switch (a) {
                            case "true":
                                n[r] = !0;
                                break;
                            case "false":
                                n[r] = !1;
                                break;
                            case "null":
                                n[r] = null;
                                break;
                            case "undefined":
                                n[r] = void 0;
                                break;
                            default:
                                n[r] = parseFloat(a);
                        }
            }
            return {op: o[1], args: n};
        }
        (t.run = function (t) {
            var n = t.match(/\S.+\S/g);
            if (n) {
                for (var r = new Array(), a = {}, i = 0, s = 0, c = n; s < c.length; s++) {
                    var l = c[s],
                        u = l.match(/^(label\d+)\:$/);
                    u ? (a[u[1]] = i) : (r.push(o(l)), i++);
                }
                var p = new Array(),
                    f = {},
                    d = new Array();
                for (i = 0; i < r.length; ) {
                    var h = r[i++];
                    switch (h.op) {
                        case "end":
                            i = r.length;
                            break;
                        case "nop":
                            break;
                        case "add":
                            var y = p.pop(),
                                g = (v = p.pop()) + y;
                            p.push(g);
                            break;
                        case "subtract":
                            (y = p.pop()), (g = (v = p.pop()) - y), p.push(g);
                            break;
                        case "multiply":
                            (y = p.pop()), (g = (v = p.pop()) * y), p.push(g);
                            break;
                        case "divide":
                            (y = p.pop()), (g = (v = p.pop()) / y), p.push(g);
                            break;
                        case "equals":
                            (y = p.pop()), (g = (v = p.pop()) == y), p.push(g);
                            break;
                        case "less":
                            (y = p.pop()), (g = (v = p.pop()) < y), p.push(g);
                            break;
                        case "and":
                            (y = p.pop()), (g = (v = p.pop()) && y), p.push(g);
                            break;
                        case "or":
                            (y = p.pop()), (g = (v = p.pop()) || y), p.push(g);
                            break;
                        case "not":
                            var v = !(y = p.pop());
                            p.push(v);
                            break;
                        case "stringEquals":
                            (y = p.pop()), (g = (v = p.pop()) == y), p.push(g);
                            break;
                        case "pop":
                            p.pop();
                            break;
                        case "toInteger":
                            (y = p.pop()), (v = parseInt(y)), p.push(v);
                            break;
                        case "getVariable":
                            (v = (y = p.pop()) in f ? f[y] : window[y]), p.push(v);
                            break;
                        case "setVariable":
                            (y = p.pop()), (f[(v = p.pop())] = y);
                            break;
                        case "stringAdd":
                            (y = p.pop()), (g = (v = p.pop()) + y), p.push(g);
                            break;
                        case "stringLess":
                            (y = p.pop()), (g = (v = p.pop()) < y), p.push(g);
                            break;
                        case "randomNumber":
                            (y = p.pop()), (y = Math.floor(Math.random() * y)), p.push(y);
                            break;
                        case "defineLocal":
                            (y = p.pop()), (f[(v = p.pop())] = y);
                            break;
                        case "callFunction":
                            v = (y = p.pop()) in f ? f[y] : window[y];
                            for (var m = p.pop(), _ = new Array(), A = 0; A < m; A++) _.push(p.pop());
                            (g = v.apply(null, _)), p.push(g);
                            break;
                        case "return_":
                            p.pop(), (i = r.length);
                            break;
                        case "modulo":
                            (y = p.pop()), (g = (v = p.pop()) % y), p.push(g);
                            break;
                        case "newObject":
                            for (
                                v = ((y = p.pop()) in f) ? f[y] : window[y], m = p.pop(), _ = new Array(), A = 0;
                                A < m;
                                A++
                            )
                                _.push(p.pop());
                            _.unshift(null), (g = new (Function.prototype.bind.apply(v, _))()), p.push(g);
                            break;
                        case "defineLocal2":
                            f[(y = p.pop())] = void 0;
                            break;
                        case "initArray":
                            A = p.pop();
                            for (var b = []; A--; ) {
                                var C = p.pop();
                                b.push(C);
                            }
                            p.push(b);
                            break;
                        case "initObject":
                            A = p.pop();
                            for (var w = {}; A--; ) (C = p.pop()), (w[(O = p.pop())] = C);
                            p.push(w);
                            break;
                        case "typeOf":
                            (v = typeof (y = p.pop())), p.push(v);
                            break;
                        case "add2":
                            (y = p.pop()), (g = (v = p.pop()) + y), p.push(g);
                            break;
                        case "less2":
                            (y = p.pop()), (g = (v = p.pop()) < y), p.push(g);
                            break;
                        case "equals2":
                            (y = p.pop()), (g = (v = p.pop()) == y), p.push(g);
                            break;
                        case "pushDuplicate":
                            (y = p.pop()), p.push(y), p.push(y);
                            break;
                        case "stackSwap":
                            (y = p.pop()), (v = p.pop()), p.push(y), p.push(v);
                            break;
                        case "getMember":
                            (y = p.pop()), (g = (v = p.pop())[y]), p.push(g);
                            break;
                        case "setMember":
                            (y = p.pop()), (v = p.pop()), ((g = p.pop())[v] = y);
                            break;
                        case "increment":
                            (y = p.pop()), y++, p.push(y);
                            break;
                        case "decrement":
                            (y = p.pop()), y--, p.push(y);
                            break;
                        case "callMethod":
                            for (y = p.pop(), v = p.pop(), m = p.pop(), _ = new Array(), A = 0; A < m; A++)
                                _.push(p.pop());
                            (g = v[y].apply(v, _)), p.push(g);
                            break;
                        case "newMethod":
                            for (y = p.pop(), v = p.pop(), m = p.pop(), _ = new Array(), A = 0; A < m; A++)
                                _.push(p.pop());
                            _.unshift(null), (g = new (Function.prototype.bind.apply(v[y], _))()), p.push(g);
                            break;
                        case "instanceOf":
                            (y = p.pop()), (g = (v = p.pop()) instanceof y), p.push(g);
                            break;
                        case "enumerate2":
                            for (var O in ((w = p.pop()), p.push(null), w)) p.push(O);
                            break;
                        case "strictEquals":
                            (y = p.pop()), (g = (v = p.pop()) === y), p.push(g);
                            break;
                        case "greater":
                        case "stringGreater":
                            (y = p.pop()), (g = (v = p.pop()) > y), p.push(g);
                            break;
                        case "storeRegister":
                            d[h.args[0]] = p[p.length - 1];
                            break;
                        case "constantPool":
                            break;
                        case "push":
                            for (var P = 0, S = h.args; P < S.length; P++) {
                                var D = S[P];
                                D instanceof e ? p.push(d[D.index]) : p.push(D);
                            }
                            break;
                        case "jump":
                            i = a[h.args[0]];
                            break;
                        case "if_":
                            (y = p.pop()) && (i = a[h.args[0]]);
                            break;
                        default:
                            console.error("未知运算符：" + h.op);
                    }
                }
                p.length > 0 && console.error("stack 有残余：" + p);
            }
        }),
            (t.getCode = o);
    })(o.AVM1 || (o.AVM1 = {}));
